// packageB/member/distribution_queue/queue/queue.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    nickname: "",
    avatar: "",
    total_activity: "",
    total_income: "",
    estimate_amount: "",
    unsettlement_amount: "",
    settlement_amount: "",
    nowithdraw_amount: "",
    withdraw_amount: "",
    failure_amount: "",
    activeName: "first",
    activity_list: [],
    status: 3, //3全部，0活动中，1已完成 ，-1失败
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    imgPath: app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commission-activity.api.activity-inquire.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: this.data.page,
        status: this.data.status,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let activity = res.data.activity;
          that.setData({
            nickname: res.data.nickname,
            avatar: res.data.avatar,
            total_activity: res.data.total_activity,
            total_income: res.data.total_income,
            estimate_amount: res.data.estimate_amount,
            unsettlement_amount: res.data.unsettlement_amount,
            settlement_amount: res.data.settlement_amount,
            nowithdraw_amount: res.data.nowithdraw_amount,
            withdraw_amount: res.data.withdraw_amount,
            failure_amount: res.data.failure_amount,
            activity_list: activity.data,
            page: activity.current_page,
            total_page: activity.last_page,
          });
          if (res.data.name) {
            wx.setNavigationBarTitle({
              title: res.data.name + "查询",
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  _getMoreData() {
    const that = this;
    let json = {
      page: this.data.page,
      status: this.data.status,
    };
    let api = "plugin.commission-activity.api.activity-inquire.index";
    let urlStr = "";
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      urlStr = app.getNetAddresss(api);
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              activity_list: that.data.activity_list.concat(res.data.activity.data),
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },
  handleClick(e) {
    let index = e.detail.index;
    if (index == 0) {
      this.data.status = "3";
    } else if (index == 1) {
      this.data.status = "0";
    } else if (index == 2) {
      this.data.status = "1";
    } else if (index == 3) {
      this.data.status = "-1";
    }
    this.getListData();
  },
  getListData() {
    let that = this;
    this.isLoadMore = false;
    this.page = 1;
    this.total_page = 0;
    this.setData({
      activity_list: [],
    });
    let urlStr = app.getNetAddresss("plugin.commission-activity.api.activity-inquire.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: this.data.page,
        status: this.data.status,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let activity = res.data.activity;
          that.setData({
            activity_list: activity.data,
            page: activity.current_page,
            total_page: activity.last_page,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  gotoCommision(e) {
    let num = e.currentTarget.dataset.num;
    wx.navigateTo({
      url: "/packageB/member/distribution_queue/DistributionCommissionDetail/DistributionCommissionDetail?tag=" + num,
    });
  },
  gotoOrder(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/member/orderdetail/orderdetail?order_id=" + id + "&orderType=" + "commission_activity",
    });
  },
  gotoParticipant(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageB/member/distribution_queue/DistributionParticipant/DistributionParticipant?id=" + id,
    });
  },
});
